/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MOs.strategy.finiteDifference.DAO;

import MOs.strategy.finiteDifference.bean.*;
import data.java.Defs;
import controller.java.Petri;

/**
 *
 * @author Erick Rubio Mendoza
 */
public class finiteDifferenceEnviromentDAO {
    public void enviromentAnalysis(finiteDifferenceMO oFDMO, finiteDifferenceEnviroment oFDEnviroment)
    {
        int countEnemies=0, countFriends=0, countCell=0;
        float eneEnemies=0.00f, eneFriends=0.00f;
        int type=0;
        float meanNutrients=0.00f, maxNutrients=0.00f, minNutrients=0.00f, countNutrients=0.00f;;
        Petri petri = Petri.getInstance();
        type=petri.getDistNutri();
        
        finiteDifferencePoint oFDAnalysisPoint = new finiteDifferencePoint();
        finiteDifferencePoint oFDMaxPoint = new finiteDifferencePoint();
        
        oFDMaxPoint.setX(oFDMO.getX());
        oFDMaxPoint.setY(oFDMO.getY());
        oFDMaxPoint.setPathBenefice(petri.getNutrient(oFDMO.getX(), oFDMO.getY()));
                        
        for (int i = 0; i < Defs.DIAMETER+1; i++) {
            for (int j = 0; j < Defs.DIAMETER+1; j++) {
                if (petri.inDish(i, j))
                {
                    countCell++;
                    countNutrients=countNutrients+petri.getNutrient(i, j);
                    if (petri.getNutrient(i, j)>maxNutrients){maxNutrients=petri.getNutrient(i, j);}
                    if (petri.getNutrient(i, j)<minNutrients){minNutrients=petri.getNutrient(i, j);}
                    
                    if (petri.getOpponent(i, j)>0)
                    {
                        countEnemies++;
                        eneEnemies=eneEnemies+petri.getEnergy(i,j);                            
                    }
                    else if(petri.getOpponent(i, j)==0)
                    {
                        countFriends++;
                        eneFriends=eneFriends+petri.getEnergy(i,j);
                    } 
                    
                    
                    //Analysis Maximum Path Benefice
                    oFDAnalysisPoint.setX(i);
                    oFDAnalysisPoint.setY(j);
                                        
                    pathBeneficeAnalysis(oFDMO, oFDAnalysisPoint);
                    
                    if (oFDAnalysisPoint.getPathBenefice() > oFDMaxPoint.getPathBenefice())
                    {
                        oFDMaxPoint.setX(oFDAnalysisPoint.getX());
                        oFDMaxPoint.setY(oFDAnalysisPoint.getY());
                        oFDMaxPoint.setPathBenefice(oFDAnalysisPoint.getPathBenefice());
                        oFDMaxPoint.setNutrientEpicentre(oFDAnalysisPoint.getNutrientEpicentre());
                    }                    
                    
                }
            }
        }
        meanNutrients = countNutrients/countCell;
        
        
        //Fill data for Enviroment
        
        oFDEnviroment.setCountEnemies(countEnemies);
        oFDEnviroment.setCountFriends(countFriends);
        oFDEnviroment.setEneEnemies(eneEnemies);
        oFDEnviroment.setEneFriends(eneFriends);
        oFDEnviroment.setMaxNutrients(maxNutrients);
        oFDEnviroment.setMinNutrients(minNutrients);
        oFDEnviroment.setMeanNutrients(meanNutrients);
        oFDEnviroment.setType(type);
        
        oFDEnviroment.setMaxBeneficePoint(oFDMaxPoint);
    }    
    
  
    
    
    private static void pathBeneficeAnalysis(finiteDifferenceMO oFDMO, finiteDifferencePoint oFDAnalysisPoint)
    {        
        float pathBenefice = 0.00f;
                      
        nutrientEpicentreAnalysis(oFDAnalysisPoint);        
        pathBenefice = oFDAnalysisPoint.getNutrientEpicentre()/9
                        - 10*Math.max(
                                    Math.abs(oFDAnalysisPoint.getX() - oFDMO.getX()),
                                    Math.abs(oFDAnalysisPoint.getY() - oFDMO.getY())
                                )*(Defs.LESS_LIVE + Defs.LESS_MOVE);        
        
        oFDAnalysisPoint.setPathBenefice(pathBenefice);                
    }    
    
    
    private static void nutrientEpicentreAnalysis(finiteDifferencePoint oFDAnalysisPoint)
    {
        Petri petri = Petri.getInstance();
        float nutrientEpicentre=0.00f;       
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {   
                    nutrientEpicentre = nutrientEpicentre + petri.getNutrient(oFDAnalysisPoint.getX()+i, oFDAnalysisPoint.getY()+j);
            }
        }
        oFDAnalysisPoint.setNutrientEpicentre(nutrientEpicentre);
    }      
    
}
